package xyz.kongbai121.common.redis.serializer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import xyz.kongbai121.common.utils.PublicUtils;
import xyz.kongbai121.common.utils.RedisKeys;
import xyz.kongbai121.modules.sys.entity.SysUserEntity;
import xyz.kongbai121.modules.sys.entity.SysUserTokenEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 用户缓存相关
 * @author kongbai121
 * @date 2021/11/12/14:55
 */
@Slf4j
@Component
public class UserCacheRunner {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取用户权限缓存
     */
    public Set<String> getUserPermissions(Long userId){
        String json = redisTemplate.opsForValue().get(RedisKeys.getSysUserPermissionsKey(String.valueOf(userId)));
        return JSON.parseObject(json, new TypeReference<Set<String>>() {});
    }

    /**
     * 设置用户权限缓存
     */
    public void setUserPermissions(Long userId, Set<String> permsSet){
        redisTemplate.opsForValue().set(RedisKeys.getSysUserPermissionsKey(String.valueOf(userId)), JSON.toJSONString(permsSet), 15, TimeUnit.MINUTES);
    }

    /**
     * 清空用户权限缓存
     */
    public void clearUserPermissions(){
        Set<String> keys = redisTemplate.keys(RedisKeys.getSysUserPermissionsKey("*"));
        if (!PublicUtils.isBlank(keys)){
            redisTemplate.delete(keys);
        }
    }

    /**
     * 获取用户token缓存
     */
    public SysUserTokenEntity getQueryByToken(String token){
        String json = redisTemplate.opsForValue().get(RedisKeys.getSysQueryByTokenKey(token));
        return JSON.parseObject(json, new TypeReference<SysUserTokenEntity>() {});
    }

    /**
     * 设置用户token缓存
     */
    public void setQueryByToken(String token, SysUserTokenEntity sysUserTokenEntity){
        redisTemplate.opsForValue().set(RedisKeys.getSysQueryByTokenKey(token), JSON.toJSONString(sysUserTokenEntity), 15, TimeUnit.MINUTES);
    }

    /**
     * 清空用户token缓存
     */
    public void clearQueryByToken(){
        Set<String> keys = redisTemplate.keys(RedisKeys.getSysQueryByTokenKey("*"));
        if (!PublicUtils.isBlank(keys)){
            redisTemplate.delete(keys);
        }
    }

    /**
     * 获取用户信息缓存
     */
    public SysUserEntity getQueryUser(Long userId){
        String json = redisTemplate.opsForValue().get(RedisKeys.getSysQueryUserKey(String.valueOf(userId)));
        return JSON.parseObject(json, new TypeReference<SysUserEntity>() {});
    }

    /**
     * 设置用户信息缓存
     */
    public void setQueryUser(Long userId, SysUserEntity sysUserEntity){
        redisTemplate.opsForValue().set(RedisKeys.getSysQueryUserKey(String.valueOf(userId)), JSON.toJSONString(sysUserEntity), 15, TimeUnit.MINUTES);
    }

    /**
     * 清空用户信息缓存
     */
    public void clearQueryUser(){
        Set<String> keys = redisTemplate.keys(RedisKeys.getSysQueryUserKey("*"));
        if (!PublicUtils.isBlank(keys)){
            redisTemplate.delete(keys);
        }
    }
}
